<template>
  <div class="divBox">
    <el-card class="box-card" shadow="never" :bordered="false">
      <div class="padding-add">
        <el-form ref="form" inline :model="tableFrom">
          <el-form-item label="标题：">
            <el-input v-model.trim="tableFrom.title" placeholder="请输入标题" size="small"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="small" @click="getList(1)">查询</el-button>
            <el-button size="small" @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    
    <el-card class="box-card mt14" shadow="never" :bordered="false">
      <el-button type="primary" class="mb20" size="small" @click="handlerAdd">新增动态</el-button>
      
      <el-table v-loading="loading" :data="tableData.data" size="small">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column label="标题" prop="title" />
        <el-table-column label="内容" prop="content" />
        <el-table-column label="图片链接" prop="picUrl" />
        <el-table-column label="语言类型" prop="languageType">
          <template slot-scope="scope">
            <el-tag :type="scope.row.languageType === 1 ? 'primary' : 'success'">
              {{ scope.row.languageType === 1 ? '中文' : '英文' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <a @click="handleDetail(scope.row)">查看详情</a>
            <el-divider direction="vertical"></el-divider>
            <a @click="handleDelete(scope.row.id)">删除</a>
          </template>
        </el-table-column>
      </el-table>
      
      <div class="block-pagination">
        <el-pagination background :page-sizes="[20, 40, 60, 80]" :page-size="tableFrom.limit" :current-page="tableFrom.page" layout="total, sizes, prev, pager, next, jumper" :total="tableData.total" @current-change="pageChange" @size-change="handleSizeChange" />
      </div>
    </el-card>

    <!-- 新增弹窗 -->
    <el-dialog title="新增动态" :visible.sync="dialogVisible" width="600px">
      <el-form :model="formData" :rules="rules" ref="formData" label-width="100px">
        <el-form-item label="标题：" prop="title">
          <el-input v-model.trim="formData.title" placeholder="请输入标题"></el-input>
        </el-form-item>
        <el-form-item label="内容：" prop="content">
          <el-input v-model.trim="formData.content" type="textarea" :rows="4" placeholder="请输入内容"></el-input>
        </el-form-item>
        <el-form-item label="图片链接：" prop="picUrl">
          <el-input v-model.trim="formData.picUrl" placeholder="请输入图片链接"></el-input>
        </el-form-item>
        <el-form-item label="语言类型：" prop="languageType">
          <el-radio-group v-model="formData.languageType">
            <el-radio :label="1">中文</el-radio>
            <el-radio :label="2">英文</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm('formData')">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { competitionDynamicSaveApi, competitionDynamicListApi, competitionDynamicDetailApi, competitionDynamicDeleteApi } from '@/api/competitionDynamic';
import { handleDeleteTable } from '@/libs/public';
import { Debounce } from '@/utils/validate';

export default {
  name: 'CompetitionDynamic',
  data() {
    return {
      tableFrom: {
        page: 1,
        limit: 20,
        title: '',
      },
      loading: false,
      dialogVisible: false,
      tableData: {
        data: [],
        total: 0,
      },
      formData: {
        title: '',
        content: '',
        picUrl: '',
        languageType: 1,
      },
      rules: {
        title: [{ required: true, message: '请填写标题', trigger: 'blur' }],
        content: [{ required: true, message: '请填写内容', trigger: 'blur' }],
        languageType: [{ required: true, message: '请选择语言类型', trigger: 'change' }],
      },
    };
  },
  mounted() {
    this.getList(1);
  },
  methods: {
    handlerAdd() {
      this.dialogVisible = true;
      this.formData = {
        title: '',
        content: '',
        picUrl: '',
        languageType: 1,
      };
    },
    resetForm() {
      this.tableFrom.title = '';
      this.getList(1);
    },
    handleDetail(row) {
      this.$message.info('查看详情功能待实现');
    },
    handleDelete(id) {
      this.$modalSure('删除该动态吗？').then(() => {
        competitionDynamicDeleteApi(id).then(() => {
          this.$message.success('删除成功');
          handleDeleteTable(this.tableData.data.length, this.tableFrom);
          this.getList();
        });
      });
    },
    getList(num) {
      this.loading = true;
      this.tableFrom.page = num ? num : this.tableFrom.page;
      competitionDynamicListApi(this.tableFrom)
        .then((res) => {
          this.tableData.data = res.list;
          this.tableData.total = res.total;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    pageChange(page) {
      this.tableFrom.page = page;
      this.getList();
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val;
      this.getList();
    },
    submitForm: Debounce(function (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          competitionDynamicSaveApi(this.formData).then((res) => {
            this.$message.success('新增成功');
            this.getList(1);
            this.dialogVisible = false;
          });
        }
      });
    }),
  },
};
</script>

<style scoped lang="scss">
.detail-content {
  .el-descriptions-item__label {
    width: 120px;
  }
}
</style> 